<?php


namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

/**
 * 公共接口
 * */
class Common extends Api
{
    protected $noNeedLogin = ['getArea', 'getQrcode'];

    /**
     * @title 银行卡绑定页面(GET)
     * @url api/common/bank
     * */
    public function bank()
    {
        // 获取后台设定的支持的银行
        $bank = Db::name('bank_category')->cache('bank')->select();
        $data = [
            'username' => $this->auth['username'],
            'bankList' => $bank
        ];
        $this->success('获取成功', $data);
    }

    /**
     * @title 获取地区(GET)
     * @url api/common/getArea
     * */
    public function getArea()
    {
        $areaData = Db::name('area')->where('level', '<=', 3)->field('id,parentid,areaname,level')->cache('area')->select();
        $province = [];
        $city = [];
        $county = [];
        foreach ($areaData as $area) {
            if ($area['parentid'] == 0 && $area['level'] == 1) {
                array_push($province, $area);
            }
            if ($area['level'] == 2) {
                array_push($city, $area);
            }
            if ($area['level'] == 3) {
                array_push($county, $area);
            }
        }
        foreach ($city as $k => $c) {
            foreach ($county as $t) {
                if ($t['parentid'] == $c['id']) {
                    $city[$k]['county'][] = $t;
                }
            }
        }
        foreach ($province as $key => $p) {
            foreach ($city as $k => $c) {
                if ($c['parentid'] == $p['id']) {
                    $province[$key]['city'][] = $c;
                }
            }
        }
        $this->success('获取成功', $province);
    }
}